package com.redhat.auth.log.core;

import java.util.Date;
import java.util.HashMap;
import java.util.Set;
import com.dayatang.domain.InstanceFactory;
import com.redhat.auth.log.common.LogDescriptionBean;
import com.redhat.auth.log.core.domain.BizLogInfo;

/**
 * 记录发生异常的日志，标志为失败日志
 * 
 * @author lingen.liu
 * 
 */
public class LogAnalysisFailure implements Runnable {

	// 业务定义
	private LogDescriptionBean logBean;

	// 日志对象存储实体
	private LogBizQuery logBizSave;

	// 异常
	private Exception exception;

	private String user;

	private BizLogInfo bizLogInfo;
	
	
	public LogAnalysisFailure(LogDescriptionBean logBean, Exception exception,BizLogInfo bizLogInfo) {
		this.logBean = logBean;
		this.exception = exception;
		this.bizLogInfo = bizLogInfo;
	}

	@Override
	public void run() {
		bizLogInfo.setBizDate(new Date());
		bizLogInfo.setBizOperation(logBean.getBizDescription());
		bizLogInfo.setBizType(logBean.getBizOperationType());
		Class[] failureClass = logBean.getFailureException();
		boolean error = true;
		for (Class failure : failureClass) {
			if (failure.equals(exception.getClass())) {
				error = false;
				break;
			}
		}
		if (error)
			bizLogInfo.setResult("Error");
		else
			bizLogInfo.setResult("Failure");
		bizLogInfo.setException(exception.getMessage());
		getLogBizSave().add(bizLogInfo);
	}

	private String getKeyValue(HashMap keyValue) {
		StringBuffer keyValueId = new StringBuffer();
		Set<String> idSet = keyValue.keySet();
		for (String id : idSet) {
			keyValueId.append(id + "=" + keyValue.get(id) + ";");
		}
		return keyValueId.toString();
	}

	public LogBizQuery getLogBizSave() {
		if (logBizSave == null)
			logBizSave = InstanceFactory.getInstance(LogBizQuery.class);
		return logBizSave;
	}

	public void setUser(String user) {
		this.user = user;
	}

}
